/**
 * Created by colin on 2017/5/7.
 */
//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
layui.use(['element','form','laydate','layer','laypage'], function () {
    var $ = layui.jquery;
    var form = layui.form();
    var element = layui.element();
    var laydate = layui.laydate;
    var layer = layui.layer;
    var laypage = layui.laypage;
    //全选
    form.on('checkbox(allChoose)', function(data){
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
        child.each(function(index, item){
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });
    form.on('submit(update-purchase)', function(data){
        var field = data.field;
        var params = $.param({
            'cpurchase.id': field.id,
            'addressId':　field.addressId,
            'cpurchase.notes' : field.notes,
            'cpurchase.orderStatus' : field.orderStatus,
            'cpurchase.orderType' : field.orderType,
            'cpurchase.paytype' : field.paytype
        },true);
        $.ajax({
            url:'/updatePurchase',
            type:'post',
            data: params,
            dataType:'json',
            success:function (data) {
                if (data!="更新失败！"){
                    layer.msg(data);
                    setTimeout(function(){
                        window.location.href = "/admin/purchase/lists";
                    }, 2000);
                }else {
                    layer.msg(data);
                }
            }
        });
    });

    var active = {
        detail : function (othis) {
            //弹出即全屏
            var index = layer.open({
                type: 2,
                title: "订单详情",
                shadeClose: true,
                content: 'detail?id='+othis.pid,
                area: ['100%', '100%'],
                maxmin: false
            });
            layer.full(index);
        },
        delete : function (othis) {
            //删除
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/deletePurchase",
                        type: 'post',
                        data: {
                            id: othis.pid
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data != "删除失败！") {
                                layer.msg(data);
                                setTimeout(function(){
                                    window.location.href = "/admin/purchase/lists";
                                }, 2000);
                            }else {
                                layer.msg(data);
                            }
                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        },
        edit2: function (item) {
            var content = $('#purchaseCars');
            layer.open({
                type: 1,
                title:'修改购买信息',
                skin: 'layui-layer-molv', //样式类名
                closeBtn: false, //不显示关闭按钮
                anim: 2,
                area: '650px;',
                btn: ['绝不后悔', '我怂了'],
                shadeClose: false, //开启遮罩关闭
                content: content,
                success: function (layero) {
                    var roleInfoItem = $(item).parents('tr').children("td");
                    $('#goodname').val(roleInfoItem[1].innerText);
                    $('#actprice').val(roleInfoItem[5].innerText);
                    $('#numbers').val(roleInfoItem[6].innerText);
                },
                yes: function (index, layero) {
                    //先序列化所有参数，再通过ajax提交数据
                    var params = $.param({
                        'buycarid': item.gid,
                        'actprice': $('#actprice').val(),
                        'numbers': $('#numbers').val(),
                    }, true);
                    $.ajax({
                        url: '/updatebuycar.action',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (data) {
                            if (data === "0") {
                                layer.msg("更新成功");
                                // window.location.href = '';
                            }else {
                                layer.msg("更新失败");
                            }
                        }
                    });
                }
            });
        },
        delete2: function (item) {
            layer.open({
                type: 1,
                title: '', //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'delete2', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 0, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除?<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/delbuycar",
                        type: 'post',
                        data: {
                            buycarid: item.gid,
                            del_flag: "1"
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "0") {
                                item.parents('tr').remove();
                                layer.msg("删除成功!");
                                // window.location.href = "/admin/purchase/lists";
                            }else {
                                layer.msg("删除失败!");
                            }

                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        }
    };
    $('.edit').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.pid = $(this).attr('pid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.delete').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.pid = $(this).attr('pid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.edit2').on('click', function () {
        var item = $(this), method = item.data('method');
        item.gid = $(this).attr('gid');
        item.pid = $(this).attr('pid');
        active[method] ? active[method].call(this, item) : '';
    });
    $('.delete2').on('click', function () {
        var item = $(this), method = item.data('method');
        item.gid = $(this).attr('gid');
        active[method] ? active[method].call(this, item) : '';
    });

    $('.closeFull').click(function(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    $('#changeAddr').click(function () {
        var content = $('.addr_form');
        $.ajax({
            url: "/purchaseAddress",
            type: "POST",
            data: {customerId: $(this).attr('custId')},
            dataType: "json",
            success: function (data) {
                var addresses = eval('('+data+')');
                if (addresses.length > 0) {
                    var addselect = $('#addr_select');
                    addresses.forEach(function (elem) {
                        // 遍历产生下拉列表
                        addselect.append('<option value="' + elem.id + '">' + elem.consignee + '--' + elem.country + '--' + elem.provience + '--' + elem.city + '--' + elem.address + '--' + elem.postcode + '</option>');
                    });
                    form.render('select');
                }

                layer.open({
                    type: 1,
                    title:'更换订单地址',
                    skin: 'layui-layer-molv', //样式类名
                    closeBtn: false, //不显示关闭按钮
                    anim: 2,
                    area: ['600px;','260px'],
                    btn: ['绝不后悔', '我怂了'],
                    shadeClose: false, //开启遮罩关闭
                    content: content,
                    yes: function (index,layero) {
                        var key = $('#addr_select').val();
                        addresses.forEach(function (elem) {
                            // 遍历产生下拉列表
                            if (elem.id == key){
                                $('.addressId').val(key);
                                $('.consignee').val(elem.consignee);
                                $('.country').val(elem.country);
                                $('.province').val(elem.province);
                                $('.city').val(elem.city);
                                $('.address').val(elem.address);
                                $('.postcode').val(elem.postcode);
                            }
                        });
                        layer.close(index);
                    }
                })
            }
        })
    });


    laypage({
        cont: 'fullpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function(obj, first){
            //得到了当前页，用于向服务端请求对应数据
            if(first!=true){//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                var condition = 'pageIndex='+pageIndex +'&';
                var customerName = $('#customerName').val();
                var map = $('#condition').val();
                var flag = false;
                if (map.length > 3){
                    flag = true;
                    map = map.substr(1,map.length-2).split(',',-1);
                    for (var i=0;i <map.length; i++){
                        condition += 'purchase.'+$.trim(map[i])+'&';
                    }
                }
                condition += 'customerName='+customerName;
                if (flag || customerName.length > 0){
                    window.location.href ="/admin/purchase/search?"+condition;
                }else {
                    window.location.href ="/admin/purchase/lists?"+condition;
                }
            }
        }
    });
});